package com.hhh.Controller;

import com.hhh.Pojo.Result;
import com.hhh.Pojo.ShoppingCart;
import com.hhh.Service.ShoppingCartSerivce;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    ShoppingCartSerivce shoppingCartSerivce;

    //    查询购物车
//    查询购物车
    @GetMapping("/list")
    public Result shoppingCart(HttpServletRequest request) {
        log.info("查询购物车");
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId((Long) request.getSession().getAttribute("user"));
        return Result.success(shoppingCartSerivce.SelectList(shoppingCart));
    }

    //    往购物车里添加菜品
//    往购物车里添加菜品
    @PostMapping("/add")
    public Result Insert(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request) {
        log.info("往购物车里添加菜品");
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setUserId((Long) request.getSession().getAttribute("user"));
        return Result.success(shoppingCartSerivce.Insert(shoppingCart));
    }

    //    从购物车里清除菜品
    //    从购物车里清除菜品
    @PostMapping("/sub")
    private Result Drop(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request) {
        log.info("从购物车里清除菜品");
        shoppingCart.setUserId((Long) request.getSession().getAttribute("user"));
        return Result.success(shoppingCartSerivce.Drop(shoppingCart));
    }

    //    清理购物车
//    清理购物车
//    清理购物车
    @DeleteMapping("/clean")
    public Result Clean(HttpServletRequest request) {
        log.info("清理购物车");
        long id = (Long) request.getSession().getAttribute("user");
        return Result.success(shoppingCartSerivce.Delete(id));
    }
}
